import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ScheduleDAO {

    // 根据ID查找场次
    public Schedule findScheduleById(String id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Schedule schedule = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM schedules WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                schedule = new Schedule();
                schedule.setId(rs.getString("id"));
                schedule.setMovieId(rs.getString("movieId"));
                schedule.setCinemaHall(rs.getString("cinemaHall"));
                schedule.setDate(rs.getString("date"));
                schedule.setTime(rs.getString("time"));
                schedule.setRemainingSeats(rs.getInt("remainingSeats"));
            }
        } catch (SQLException e) {
            System.out.println("查找场次失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return schedule;
    }

    // 根据电影ID查找场次
    public List<Schedule> findSchedulesByMovieId(String movieId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Schedule> schedules = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM schedules WHERE movieId = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, movieId);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Schedule schedule = new Schedule();
                schedule.setId(rs.getString("id"));
                schedule.setMovieId(rs.getString("movieId"));
                schedule.setCinemaHall(rs.getString("cinemaHall"));
                schedule.setDate(rs.getString("date"));
                schedule.setTime(rs.getString("time"));
                schedule.setRemainingSeats(rs.getInt("remainingSeats"));
                schedules.add(schedule);
            }
        } catch (SQLException e) {
            System.out.println("查找场次失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return schedules;
    }

    // 获取所有场次
    public List<Schedule> getAllSchedules() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Schedule> schedules = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM schedules";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Schedule schedule = new Schedule();
                schedule.setId(rs.getString("id"));
                schedule.setMovieId(rs.getString("movieId"));
                schedule.setCinemaHall(rs.getString("cinemaHall"));
                schedule.setDate(rs.getString("date"));
                schedule.setTime(rs.getString("time"));
                schedule.setRemainingSeats(rs.getInt("remainingSeats"));
                schedules.add(schedule);
            }
        } catch (SQLException e) {
            System.out.println("获取所有场次失败: " + e.getMessage());
        } finally {
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return schedules;
    }

    // 添加场次
    public boolean addSchedule(Schedule schedule) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO schedules (id, movieId, cinemaHall, date, time, remainingSeats) VALUES (?, ?, ?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, schedule.getId());
            pstmt.setString(2, schedule.getMovieId());
            pstmt.setString(3, schedule.getCinemaHall());
            pstmt.setString(4, schedule.getDate());
            pstmt.setString(5, schedule.getTime());
            pstmt.setInt(6, schedule.getRemainingSeats());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("添加场次失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 更新场次信息
    public boolean updateSchedule(Schedule schedule) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE schedules SET movieId = ?, cinemaHall = ?, date = ?, time = ?, remainingSeats = ? WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, schedule.getMovieId());
            pstmt.setString(2, schedule.getCinemaHall());
            pstmt.setString(3, schedule.getDate());
            pstmt.setString(4, schedule.getTime());
            pstmt.setInt(5, schedule.getRemainingSeats());
            pstmt.setString(6, schedule.getId());

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("更新场次失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 更新剩余座位数
    public boolean updateRemainingSeats(String scheduleId, int remainingSeats) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE schedules SET remainingSeats = ? WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, remainingSeats);
            pstmt.setString(2, scheduleId);

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("更新剩余座位失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 删除场次
    public boolean deleteSchedule(String id) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM schedules WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, id);

            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            System.out.println("删除场次失败: " + e.getMessage());
            return false;
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                DBUtil.closeConnection(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}